package com.ruoyi.binance.api;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.binance.domain.BinanceTransactionOrder;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedHashMap;

public class OrderApi {
    private static final Logger log = LoggerFactory.getLogger(OrderApi.class);

    public static JSONObject order(BinanceTransactionOrder order) {
        try {
            LinkedHashMap<String, Object> paramMap = new LinkedHashMap<>();
            paramMap.put("symbol", order.getCode());
            paramMap.put("side", order.getSide());
            paramMap.put("type", order.getType());
            if (order.getQuantity() != null) {
                paramMap.put("quantity", order.getQuantity());
            }
            if (order.getQuoteOrderQty() != null) {
                paramMap.put("quoteOrderQty", order.getQuoteOrderQty());
            }
            paramMap.put("newClientOrderId", order.getOrgOrderId());
            paramMap.put("newOrderRespType", "ACK");
            paramMap.put("recvWindow", Config.RECV_WINDOW);
            paramMap.put("timestamp", System.currentTimeMillis());
            paramMap.put("signature", SignatureUtils.getSignature(paramMap));

            String body = HttpRequest.post(Config.BASE_URL + Config.ORDER_URL)
                    .header("X-MBX-APIKEY", Config.API_KEY)
                    .form(paramMap)
                    .timeout(20000)//超时，毫秒
                    .execute().body();

            if (StringUtils.isBlank(body)) {
                return new JSONObject();
            }
            log.info("代码：{}，订单号：{}，下单返回：{}", order.getCode(), order.getOrgOrderId(), body);
            JSONObject entries = JSONObject.parseObject(body);
            return entries == null ? new JSONObject() : entries;
        } catch (Exception e) {
            log.error("下单异常：{}", e.getMessage());
        }

        return new JSONObject();
    }


    public static JSONObject queryOrder(String code, String orgOrderId) {
        LinkedHashMap<String, Object> paramMap = new LinkedHashMap<>();
        paramMap.put("symbol", code);
        paramMap.put("origClientOrderId", orgOrderId);
        paramMap.put("recvWindow", Config.RECV_WINDOW);
        paramMap.put("timestamp", System.currentTimeMillis());
        paramMap.put("signature", SignatureUtils.getSignature(paramMap));

        String body = HttpRequest.get(Config.BASE_URL + Config.ORDER_URL)
                .header("X-MBX-APIKEY", Config.API_KEY)
                .form(paramMap)
                .timeout(20000)//超时，毫秒
                .execute().body();

        if (StringUtils.isBlank(body)) {
            throw new RuntimeException("请求网络异常！");
        }
        JSONObject entries = JSONObject.parseObject(body);
        return entries == null ? new JSONObject() : entries;
    }

    public static void main(String[] args) {
//        BinanceTransactionOrder order = new BinanceTransactionOrder();
//        order.setCode("XVGUSDT");
//        order.setQuantity(new BigDecimal(2000));
//        order.setOrgOrderId(IdUtil.getSnowflakeNextIdStr());
//        order.setSide("SELL");
//        order.setType("MARKET");
//        order.setCreateTime(new Date());
//        order.setRemark("自动交易");
//        System.out.println(order);
//        order(order);
        System.out.println(queryOrder("LINKUSDT", "1955579166641090560"));
    }
}
